package cn.edu.cqupt.cxcy.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.cqupt.cxcy.dto.AdminImpl;
import cn.edu.cqupt.cxcy.dto.InvestorImpl;
import cn.edu.cqupt.cxcy.model.Field;
import cn.edu.cqupt.cxcy.model.Project;
import cn.edu.cqupt.cxcy.model.ProjectPage;


/**
 * 主页-【场地预约】
 * @author Dancer-mno
 * @date 2015年10月17日
 */
@SuppressWarnings("serial")
public class InveHaveBean extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String pjax_back = "$(function(){$(\"a.turn_pjax[href='inve/havebeen']\").trigger('click');});";
		if(req.getHeader("x-pjax") == null || req.getHeader("x-pjax").equals("null")) {
			req.setAttribute("pjax_back", pjax_back);
			req.getRequestDispatcher("investor.jsp").forward(req, resp);
		} else {
			
			
			HttpSession session=req.getSession();
			List<Object> list=(List<Object>)session.getAttribute("user");
			int inve_id=(int)list.get(1);
		List<Project> show =new InvestorImpl().returnProjectByid(inve_id,1);
		 
		 if(show.size()!=0)
		 {List<Map<String,String>> info=new ArrayList<Map<String,String>>();
		 int firstpagesum=1;
	 		if(show.size()%6==0)
	 			firstpagesum=show.size()/6;
	 		else
	 			firstpagesum=(show.size()/6)+1;
	 		ProjectPage page=new ProjectPage(firstpagesum,1,6);
        String pagenow=req.getParameter("pagenow");
	    	
	    	if(pagenow!=null)
	    		page.setPagenow(Integer.parseInt(pagenow));
	    	//System.out.println(all.getPagenow());
	    	String type=req.getParameter("type");
	    	if(type!=null)
	    	{if("up".equals(type))
	    		{page.setPagenow(page.getPagenow()-1);
	    		}
	    	if("down".equals(type))
	    			page.setPagenow(page.getPagenow()+1);
	    		
	    	}
	    	
			 for(int i=(page.getPagenow()-1)*6;i<6*page.getPagenow()-1;i++)
		 {Map<String,String> map=new HashMap<String,String>();
		 if(i<=show.size()-1)
		 { map.put("pro_id", Integer.toString(show.get(i).getP_id()));
		 map.put("pro_name",show.get(i).getP_name());
		 map.put("pro_acount", Integer.toString(new InvestorImpl().returnacount(inve_id, show.get(i).getP_id(),2)));
		 info.add(map); }
		 }
	     req.setAttribute("pageinfo", page);
		 req.setAttribute("pro_info", info);
		 
		 }
		req.getRequestDispatcher("yitou.jsp").forward(req, resp);
		}
		} 	
	
	
	
}
